<?php

class Security {
	
	// ============= SESSION =================== //
	public static function sessionExists($key) {
		return (isset($_SESSION[$key]))? true : false;
	}
	public static function setSession($key, $value) {
		return $_SESSION[$key] = $value;
	}
	public static function getSession($key) {
		return $_SESSION[$key];
	}
	public static function unsetSession($key) {
		if (self::sessionExists($key)) {
			unset($_SESSION[$key]);
		}
	}
	// ============= COOKIE =================== //
	public static function cookieExists ($key) {
		return (isset($_COOKIE[$key]))? true : false;
	}
	public static function getCookie($key) {
		return $_COOKIE[$key];
	}	
	public static function setCookie($key, $value, $expiry = 3600, $path) {
		if (setcookie($key, $value, time() + $expiry, $path)) {
			return true;
		} else {
			return false;
		}
	}
	public static function unsetCookie($key, $path) {
		return setcookie($key, "", time() - 1, $path);
	}
	// ============= CRYPT =================== //
	public static function cryptString($input, $salt = '') {
		return (hash('sha256', $input.$salt));
	}
	public static function getSalt($length) {
		return mcrypt_create_iv($length);
	}
	public static function matchString($src, $input, $salt) {
		$tbc = self::cryptString($src,$salt);

		if ($input === $tbc) {
			return true;
		} else {
			return false;
		}
	}
	// ============= HTML =================== //
	public static function sanitize($inp) {
		return htmlentities($inp);
	}
	
	// ============== REQ =================== //
	public static function inputExists($type = 'post') {
		switch($type) {
			case 'post':
				return (!empty($_POST)) ? true : false;
			break;
			case 'get':
				return (!empty($_GET)) ? true: false;
			break;
			default:
				return false;
			break;
		}
	}

	public static function inputRead($item) {
		if(isset($_POST[$item])) {
			return $_POST[$item];
		} else if (isset($_GET[$item])) {
			return $_GET[$item];
		}
		
		return '';
	}
}


?>